Implicit vs Explicit types
In C#, the var keyword is used for implicit typing, meaning that the data type of a variable is inferred based on the value that is assigned to it. This can be useful for reducing redundancy and making code more concise, but it can also have some drawbacks.
Here are some technical details to consider when using var over explicit type declarations:
- Type inference: When using
var, the compiler will automatically determine the data type of the variable based on the value that is assigned to it. This can be helpful when working with complex types, as it saves you from having to explicitly declare the type. - Readability: Using
varcan make code more concise and easier to read in some cases, but it can also make code harder to follow if the inferred type is not immediately clear. - Scope: The scope of a variable declared with
varis the same as if it were declared with an explicit type. The variable's type is determined at compile time, and once it's assigned a value, it cannot be changed. - Performance: There is generally no difference in performance between using
varand explicit type declarations. The compiled code will be the same either way. - Initialization: When using
var, the variable must be initialized at the same time it is declared, as the compiler needs to determine its type based on the assigned value.
Overall, using var can be a useful tool for reducing redundancy and making code more concise. However, it's important to use it judiciously and ensure that your code remains readable and easy to follow. It's also important to be aware of the limitations and potential drawbacks of using var, such as reduced readability or unexpected type inference.
We don't prefer "var" over explicit types in sake of readibility on pull requests. Please consider NOT using var over explicit types.
// Instead of this...
var userFeePercentages = await _userFeeApiProxy.QueryUserFeePercentages(userId);
// Use this.
List<UserFeePercentage> = await _userFeeApiProxy.QueryUserFeePercentages(userId);